package com.hn.zhijian.web.config.shiro;

import com.hn.zhijian.utils.Md5Utils;
import com.hn.zhijian.web.config.jwt.JwtExecute;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Function:shrio密码校验扩展类. <br/>
 *
 * @author carl chen
 * 验证token是否一致
 * @ConditionalOnMissingBean 注解默认注入，如果有相同只取最后一个
 */
public class CustomizedCredentialsMatcher extends SimpleCredentialsMatcher {

    private JwtExecute jwtExecute;

    public CustomizedCredentialsMatcher(JwtExecute jwtExecute) {
        this.jwtExecute = jwtExecute;
    }

    @Override
    public boolean doCredentialsMatch(AuthenticationToken authToken, AuthenticationInfo info) {
        //验证token
        String token = (String) authToken.getCredentials();
        return jwtExecute.verify(token, jwtExecute.getUsername(token));

    }

    /**
     * 将传进来密码加密方法
     *
     * @param data
     * @return
     */
    private String encrypt(String data) {
        return Md5Utils.getStringMD5(data);
    }
}
